package com.dev.web.controller.safe;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.domain.BbsUserCollect;
import com.dev.bbs.service.IBbsUserCollectService;
import com.dev.common.annotation.Log;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.common.core.page.TableDataInfo;
import com.dev.common.enums.BusinessType;
import com.dev.framework.util.ShiroUtils;

/**
 * 用户文章收藏Controller
 *
 * @author dev
 * @date 2019-09-23
 */
@Controller
@RequestMapping("/safe/collect")
public class SafeCollectController extends BaseController
{
    @Autowired
    private IBbsUserCollectService bbsUserCollectService;

    /**
    * 查询用户文章收藏列表
    */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BbsUserCollect bbsUserCollect)
    {
        startPage();
        bbsUserCollect.setUserId(ShiroUtils.getUserId());
        List<BbsUserCollect> list = bbsUserCollectService.selectBbsUserCollectList(bbsUserCollect);
        return getDataTable(list);
    }

    /**
     * 新增保存用户文章收藏
     */
    @Log(title = "用户文章收藏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BbsUserCollect bbsUserCollect)
    {
        long userId = ShiroUtils.getUserId();
        return toAjax(bbsUserCollectService.insertBbsUserCollect(bbsUserCollect.setUserId(userId)));
    }

    /**
    * 删除用户文章收藏
    */
    @Log(title = "用户文章收藏", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam(value="artId",required=true)Long artId)
    {
        long userId = ShiroUtils.getUserId();
        return toAjax(bbsUserCollectService.uncollect(userId, artId));
    }
}
